
package com.epam.android.training.app.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Class for creating/updating db
 * 
 * @author Natalie
 */
public class FeedsOpenHelper extends SQLiteOpenHelper {
  private static final int DATABASE_VERSION = 5;

  private static final String DATABASE_NAME = "customnewsdb";

  public static final String FEEDS_TABLE_NAME = "feeds";

  public static final String FEEDS_ITEMS_TABLE_NAME = "items";

  public static final String ITEMID_FIELD = "item_id";

  public static final String TITLE_FIELD = "title";

  public static final String URL_FIELD = "url";

  public static final String DATE_FIELD = "news_date";

  private static final String FEEDS_TABLE_CREATE = "CREATE TABLE " + FEEDS_TABLE_NAME
      + " (feed_id integer primary key autoincrement," + TITLE_FIELD + " text not null," + URL_FIELD
      + " text not null);";

  private static final String FEEDS_ITEMS_TABLE_CREATE = "CREATE TABLE " + FEEDS_ITEMS_TABLE_NAME + " (" + ITEMID_FIELD
      + " integer primary key autoincrement," + TITLE_FIELD + " text not null," + DATE_FIELD + " INTEGER not null, "
      + URL_FIELD + " text not null);";

  public FeedsOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    db.execSQL(FEEDS_TABLE_CREATE);
    db.execSQL(FEEDS_ITEMS_TABLE_CREATE);

  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + FEEDS_TABLE_NAME);
    db.execSQL("DROP TABLE IF EXISTS " + FEEDS_ITEMS_TABLE_NAME);
    onCreate(db);
  }

}
